<?php

namespace App\Exports;

use App\Models\Asset;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\DefaultValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class AssetInputTemplateExport extends DefaultValueBinder implements WithColumnFormatting, FromCollection, WithMapping, WithHeadings
{

    public function collection()
    {
        $simpleAsset = new Asset([
            "name" => "必填",
            "category_id" => '必填',
            "sub_category_id" => '必填',
            "brand_id" => '必填',
            "status_id" => '必填',
            "affiliation_id" => '必填',
            "keeper_id" => '必填'
        ]);

        return collect([$simpleAsset]);
    }

    public function bindValue(Cell $cell, $value)
    {
        if (is_numeric($value)) {
            $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);

            return true;
        }
        return parent::bindValue($cell, $value); // TODO: Change the autogenerated stub
    }

    /**
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_TEXT,
        ];
    }

    /**
     * @param mixed $row
     *
     * @return array
     */
    public function map($row): array
    {
//        $row = new Asset;
        return [
            $row->name,
            $row->product_model_number,
            $row->category_id,
            $row->sub_category_id,
            $row->brand_id,
            $row->sn,
            $row->status_id,
            $row->affiliation_id,
            $row->keeper_id,
            $row->marking_price,
            $row->bid_price,
            $row->bid_date,
            $row->vendor_id,
            $row->location_id,
        ];
    }

    /**
     * @return array
     */
    public function headings(): array
    {
        return [
            '名称',
            '物品编码',
            '主类',
            '副类',
            '品牌',
            'SN序列号',
            '状态',
            '归属',
            '保管人',
            '市场价',
            '购入价',
            '购入日期',
            '供应商',
            '仓库',
        ];
    }
}
